import { jsonArray } from '@/api/productApi/file'
import Vue from 'vue'

/* 此文件用于将数据转换为中文*/

export function dataConversionMethod(data, enumData, specialTypeList, newSpaperList) {
	// 基金经理证件类型
	const fundMange = [
		{
			key: '0113',
			value: '外国人永久居留证'
		},
		{
			key: '0199',
			value: '其他人员证件'
		},
		{
			key: '0112',
			value: '外国护照'
		},
		{
			key: '0111',
			value: '社会保障号'
		},
		{
			key: '0110',
			value: '警官证'
		},
		{
			key: '0109',
			value: '文职证'
		},
		{
			key: '0108',
			value: '户口本'
		},
		{
			key: '0107',
			value: '士兵证'
		},
		{
			key: '0106',
			value: '军官证'
		},
		{
			key: '0105',
			value: '港澳台居民居住证'
		},
		{
			key: '0101',
			value: '身份证'
		},
		{
			key: '0102',
			value: '护照'
		},
		{
			key: '0103',
			value: '港澳居民来往内地通行证'
		},
		{
			key: '0104',
			value: '台湾居民来往大陆通行证'
		}
	]

	// 费率计时提点
	const rateCheck = [
		{ typeCode: '1', typeName: '不提取' },
		{ typeCode: '2', typeName: '资产管理计划投资者赎回时' },
		{ typeCode: '3', typeName: '资产管理计划分红时计提' },
		{ typeCode: '4', typeName: '资产管理计划固定开放日计提' },
		{ typeCode: '5', typeName: '资产管理计划临时开放日计提' },
		{ typeCode: '6', typeName: '资产管理计划到期计提' },
		{ typeCode: '7', typeName: '其他计提时点' }
	]

	// 所有枚举字段
	const enumProperty = ['certificateType', 'csrcType', 'associationOpenFrequency', 'productAssetReportType', 'addCreditOrganType', 'addCreditFormType', 'investAdvisorOrgan', 'operateMode', 'openFrequency', 'fiduciaryDuty', 'settleCurrency', 'fundType', 'mixedInvestBias', 'pobcFundType', 'riskGrade', 'isGradFund', 'revenueFeature', 'valuationWay', 'valuationFrequency', 'marketFundType', 'marketTradeSite', 'investTactics', 'feeRateAlgorithm', 'planType', 'productAssetType', 'annuityPlanType', 'annuityTrusteeMode', 'addcreditMeasureType', 'privateFundType', 'peopleMax', 'productSpecialType', 'incomeDisWay']

	// 是/否单选所有字段
	const radioList = ['crossBorderFinancial', 'propertyFundTrust', 'noneHKTIsStopApplyRansom', 'isAssetManagementProduct', 'isAssetSecuritizationBusiness', 'isSpecialAssetReport', 'isInnovateNature', 'isHoldFund', 'isListTransfer', 'isQdii', 'isIndexFund', 'isBreakeven', 'isMutualFund', 'isActiveManage', 'managerStyle', 'issuerEarlyTermFlag', 'usufructTransferProductFlag', 'currencyMarketFundFlag', 'isAddcreditMeasure', 'isMom', 'isSubAssets', 'isNetWorthProduct', 'isRiskTacticsFund', 'isMarketTrade', 'isPensionTargetFund', 'isEtfJoinFund', 'isFofFund', 'isSponsoredFund', 'isOrganCustom', 'isSingleShare', 'isContainSpclLimit', 'isQuantTactics', 'isCoverTactics', 'isThirdPartyCustody', 'isAchieveReward', 'isHolderMeet', 'isChannelBusiness', 'isSamp', 'isPrivateFund', 'isPensionProduct', 'isInstAcc', 'isBasicPension', 'isSocialSecurity', 'isOccupationalPension', 'isAnnuityPension', 'isStructuredProduct', 'isRiskMeasures']

	// 有/无单选所有字段
	const radioWithWithout = ['customerRedemptionFlag', 'earlyWarnLine', 'stopLossLine', 'maxRetreat', 'otherRiskStep', 'managerIsInvestment', 'managerIsPartake', 'stopLossWarningLine']

	// 管理方式
	const managerStyle = ['managerStyle']

	data.forEach(items => {
		for (const i in items) {
			if (Object.prototype.toString.call(items[i]) === '[object Object]') {
				const obj = items[i]
				const itemProperty = Object.keys(obj)
				itemProperty.forEach(item => { // 处理单选是否
					if (radioList.includes(item)) {
						obj[item] = obj[item] ? obj[item] === '0' ? '是' : '否' : ''
					}
					if (radioWithWithout.includes(item)) {
						obj[item] = obj[item] ? obj[item] === '0' ? '有' : '无' : ''
					}
					if (managerStyle.includes(item)) {
						obj[item] = obj[item] ? obj[item] === '0' ? '集合管理' : '单独管理' : ''
					}
					if (item === 'accrualPointTime') { // 处理费率计时提点
						if (obj[item]) {
							const list = obj[item].split(',')
							const msg = []
							list.forEach(value => {
								msg.push(rateCheck.find(rate => rate.typeCode === value).typeName)
							})
							obj[item] = msg.join(',')
						}
					}
					if (item === 'specialTypeChar') { // 特殊品种
						if (obj[item]) {
							const specialTypeCharList = obj[item].split(',')
							const itemList = []
							specialTypeCharList.forEach(special => {
								const itemObj = specialTypeList.find(it => it.specialTypeId === special)
								itemList.push(itemObj ? itemObj.specialTypeName : special)
							})
							if (itemList.length) {
								obj[item] = [...jsonArray(itemList)].join(',')
							}
						}
					}
					if (enumProperty.includes(item)) {
						if (obj[item]) {
							if (['productSpecialType', 'incomeDisWay'].includes(item)) { // 数组类型枚举
								const list = obj[item].split(',')
								const msg = []
								list.forEach(value => {
									msg.push(enumData[item].find(enumItem => enumItem.typeCode === value).typeName)
								})
								obj[item] = msg.join(',')
							} else { // 字符串型枚举
								const enumItem = enumData[item].find(enumItem => enumItem.typeCode === obj[item])
								obj[item] = enumItem ? enumItem.typeName : ''
							}
						}
					}
					if (item === 'newspaperId') { // 报社
						if (obj[item]) {
							const msg = newSpaperList.find(val => val.newspaperId === obj[item]) ? newSpaperList.find(val => val.newspaperId === obj[item]).newspaper : ''
							obj[item] = msg
						} else {
							obj[item] = ''
						}
					}
				})
				if (i === 'baseInfo') {
					if (Array.isArray(obj.branchShareList) && obj.branchShareList.length) {
						obj.branchShareList.forEach(item => {
							item.isItSuitable = item.estimatedAnnualRate ? '适用' : '不适用'
							if (item.shareCategory) {
								item.shareCategory = enumData.shareCategory.find(obj => obj.typeCode === item.shareCategory).typeName
							}
						})
					}
				}
				if (i === 'rateInfo') {
					if (obj) {
						if (Array.isArray(obj.feeRateInfoList) && obj.feeRateInfoList.length) {
							obj.feeRateInfoList.forEach(item => {
								if (obj.feeRateType === '9') {
									if (obj.feeRateAlgorithm) {
										item.feeRateAlgorithm = obj.feeRateAlgorithm
									}
								}
								if (item.feeRateType === '7') {
									if (obj.custodyFeeAlgorithm) {
										item.custodyFeeAlgorithm = enumData.custodyFeeAlgorithm.find(items => items.typeCode === obj.custodyFeeAlgorithm).typeName
									}
								}
								if (item.feeRateType === '8') {
									if (item.salesServiceFeeAlgorithm) {
										item.salesServiceFeeAlgorithm = enumData.salesServiceFeeAlgorithm.find(items => items.typeCode === item.salesServiceFeeAlgorithm).typeName
									}
								}
							})
						}
					}
				}
				items[i] = obj
			} else if (i === 'fundManagerInfoList') { // 处理基金经理证件类型
				if (!!items[i] && Array.isArray(items[i]) && items[i].length) {
					const fundManagerInfoList = items[i]
					fundManagerInfoList.forEach(mange => {
						if (mange.idType) {
							if (![...mange.idType].some(str => str.charCodeAt(0) > 255)) {
								mange.idType = mange.idType ? fundMange.find(value => value.key === mange.idType).value : ''
							}
						}
					})
				}
			}

			if (i === 'baseInfo') { // 处理下属基金
				if (!!items[i].branchShareList && Array.isArray(items[i].branchShareList)) {
					items[i].branchShareList.forEach(sub => {
						if (sub.subFundType) {
							if (![...sub.subFundType].some(str => str.charCodeAt(0) > 255)) {
								sub.subFundType = enumData.subFundType.find(obj => obj.typeCode === sub.subFundType).typeName
							}
						}
					})
				}
			}
		}
	})
	data.forEach(item => {
		if (item.rateInfo) {
			if ('feeRateAlgorithm' in item.rateInfo) {
				Vue.delete(item.rateInfo, 'feeRateAlgorithm')
			}
			if ('custodyFeeAlgorithm' in item.rateInfo) {
				Vue.delete(item.rateInfo, 'custodyFeeAlgorithm')
			}
		}
	})

	return data
}

export const EnumList = [
	{
		propertyKey: 'investAdvisorOrgan',
		enumKey: 'investAdvisorOrgan'
	},
	{
		propertyKey: 'csrcType',
		enumKey: 'csrcTypeOrgan'
	},
	{
		propertyKey: 'certificateType',
		enumKey: 'idTypeOrgan'
	},
	{
		propertyKey: 'operateMode',
		enumKey: 'operateMode'
	},
	{
		propertyKey: 'openFrequency',
		enumKey: 'openFrequency'
	},
	{
		propertyKey: 'fiduciaryDuty',
		enumKey: 'fiduciaryDuty'
	},
	{
		propertyKey: 'settleCurrency',
		enumKey: 'settleCurrency'
	},
	{
		propertyKey: 'fundType',
		enumKey: 'fundType'
	},
	{
		propertyKey: 'mixedInvestBias',
		enumKey: 'mixedInvestBias'
	},
	{
		propertyKey: 'pobcFundType',
		enumKey: 'fundType'
	},
	{
		propertyKey: 'riskGrade',
		enumKey: 'riskGrade'
	},
	{
		propertyKey: 'isGradFund',
		enumKey: 'gradFundType'
	},
	{
		propertyKey: 'revenueFeature',
		enumKey: 'revenueFeature'
	},
	{
		propertyKey: 'valuationWay',
		enumKey: 'valuationWay'
	},
	{
		propertyKey: 'valuationFrequency',
		enumKey: 'valuationFrequency'
	},
	{
		propertyKey: 'marketFundType',
		enumKey: 'marketFundType'
	},
	{
		propertyKey: 'marketTradeSite',
		enumKey: 'marketTradeSite'
	},
	{
		propertyKey: 'investTactics',
		enumKey: 'investTactics'
	},
	{
		propertyKey: 'feeRateAlgorithm',
		enumKey: 'feeRateAlgorithm'
	},
	{
		propertyKey: 'planType',
		enumKey: 'planType'
	},
	{
		propertyKey: 'productAssetType',
		enumKey: 'productAssetType'
	},
	{
		propertyKey: 'annuityPlanType',
		enumKey: 'annuityPlan'
	},
	{
		propertyKey: 'annuityTrusteeMode',
		enumKey: 'annuityTrusteeMode'
	},
	{
		propertyKey: 'addcreditMeasureType',
		enumKey: 'addcreditMeasure'
	},
	{
		propertyKey: 'addCreditFormType',
		enumKey: 'addCreditFormType'
	},
	{
		propertyKey: 'addCreditOrganType',
		enumKey: 'addCreditOrganType'
	},
	{
		propertyKey: 'privateFundType',
		enumKey: 'privateFundType'
	},
	{
		propertyKey: 'peopleMax',
		enumKey: 'peopleMax'
	},
	{
		propertyKey: 'productAssetReportType',
		enumKey: 'productAssetReportType'
	},
	{
		propertyKey: 'associationOpenFrequency',
		enumKey: 'associationOpenFrequency'
	},
	{
		propertyKey: 'productSpecialType',
		enumKey: 'productSpecialType',
		type: 'stringArray'
	},
	{
		propertyKey: 'incomeDisWay',
		enumKey: 'incomeDisWay',
		type: 'stringArray'
	}
]

// 风控排序
export const riskInfoSort = {
	isRiskMeasures: '是否有风控措施：预警、止损、最大回撤及其他风控措施等',
	investLeverRate: '投资杠杆率上限（总资产/净资产）%',
	earlyWarnLine: '有无预警线',
	earlyWarnLineType: '预警线类型',
	earlyWarnNetWorth: '预警线对应的资产管理计划净值',
	stopLossLine: '有无止损线',
	stopLossLineType: '止损线类型',
	stopLossNetWorth: '止损线对应的资产管理计划净值',
	maxRetreat: '有无回撤预警线',
	retreatWarnLineType: '回撤预警线类型',
	retreatEarlyWarnNetWorth: '回撤预警线对应的资产管理计划净值',
	stopLossWarningLine: '有无回撤止损线',
	retreatStopLossLineType: '回撤止损线类型',
	retreatStopLossNetWorth: '回撤止损线对应的资产管理计划净值',
	otherRiskStep: '有无其他风控措施',
	otherRiskExpression: '其他风控措施-合同表述',
	managerIsInvestment: '管理人是否投入资金',
	managerInvestmentAmount: '管理人投入资金参与金额',
	managerInvestmentRatio: '管理人投入资金参与比例',
	managerIsPartake: '管理人附属机构自有资金是否参与',
	managerPartakeAmount: '管理人及其附属机构自有资金参与金额（万元）',
	managerPartakeRatio: '管理人及其附属机构自有资金参与比例（%）'
}
